package com.sto.common.generator;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * mysql 代码生成器演示例子
 * </p>
 *
 * @author jobob
 * @since 2018-09-12
 */
public class MysqlGenerator {

    /**
     * RUN THIS
     */
    public static void main(String[] args) {
        List<String> tableNames = new ArrayList<>();
        tableNames.add("sky_forwarder_administrator");
        batchCreateTable("", tableNames);
    }


    public static void batchCreateTable(String moduleName, List<String> tableNames) {
        for (String tableName : tableNames) {
            createTable(moduleName, tableName);
        }
    }


    /**
     * 创建表
     */
    public static void createTable(String moduleName, String tableName) {
        //运力库
        /*DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder("jdbc:mysql://rm-1hht058q4779l55jp.mysql.rds.aliyuncs.com:3306/sto_transport_capability?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true",
                "transport_cap", "Transport_cap8937")*/
                //运输库
                        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder("jdbc:mysql://rm-1hht058q4779l55jp.mysql.rds.aliyuncs.com:3306/transport_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai",
                                "tms_test", "Tms_test123")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());
        FastAutoGenerator.create(dataSourceConfigBuilder)
                .globalConfig(builder ->
                    builder.author("ChenPeng") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir") + "/common/src/main/java") // 指定输出目录
                )
                .packageConfig(builder ->
                    builder.parent("com.sto.common.driverCost") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mappers")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "/common/src/main/resources/mappers")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .entityBuilder()
                            .enableLombok()
                            .disableSerialVersionUID()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            //.superClass("com.sto.common.driverCost.entity.BaseEntity")
                            //.addSuperEntityColumns("gmt_create", "gmt_modified", "creator", "editor")
                            .serviceBuilder()
                            .formatServiceFileName("I%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .build()
                )
                .templateConfig(builder ->
                    builder.controller("")
                            .build()
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}

